package com.hzzy.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hzzy.bean.BaseInfo;
import com.hzzy.bean.HzDormMember;
import com.hzzy.bean.HzDormitoryCheckins;
import com.hzzy.bean.HzTeacher;
import com.hzzy.enums.UserSessionKeyEnum;
import com.hzzy.service.HzCheckDormService;
import com.hzzy.service.HzCommonService;

/**
 * 寝室点到
 * 
 * @author LSJ 2016年5月12日
 */
@Controller
@RequestMapping("/dormCheck")
public class CheckDormitoryController {

	@Resource
	private HzCheckDormService hzCheckDormService;
	@Resource
	private HzCommonService hzCommonService;

	@RequestMapping(value = "/queryDormMenber")
	public String queryDormMember(Model model, HttpServletRequest request) {
		BaseInfo baseInfo  =(BaseInfo) request.getSession().getAttribute(UserSessionKeyEnum.BASE_USER_INFO.getValue());
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
		String time = df.format(new Date());
		int num=0;
		List<HzDormMember> members;
		List<HzTeacher> tlist=null;
		List<HzTeacher> contactlist=null;
		Map<String, Object> param = new HashMap();
		if (!baseInfo.isStudentUser()) return "dormitorycheck.page";
		param.put("studentid", baseInfo.getStudent().getSno());
		param.put("date",time);

		members = hzCheckDormService.queryDormMember(param);
		Map<String, List<Object>> queryTparam = new HashMap();
		List<Object> temps = new ArrayList<Object>();
		int tempint = 0;
		for (HzDormMember temp : members) {
			temp.setId(++tempint + "");
			temps.add(temp.getClassId());
		}
		queryTparam.put("clist", temps);
		if(temps.size()!=0){
			tlist = hzCommonService.queryTeachersByClassid(queryTparam);
			for (int i = 0; i < tlist.size(); i++) {
				num++;
				tlist.get(i).setId(num);
				tlist.get(i).setClassName(tlist.get(i).getClassName() + "(班主任)");
			}
		}
		if(param.size()!=0){
			contactlist = hzCheckDormService.queryDormContactByStudentId(param);
			for (int i = 0; i < contactlist.size(); i++) {
				num++;
				contactlist.get(i).setId(num);
				contactlist.get(i).setClassName("寝室联系人");
			}
		}

		model.addAttribute("teacherlist", tlist);
		model.addAttribute("contactlist", contactlist);
		model.addAttribute("members", members);
		
		List checkins=hzCheckDormService.checkDormCheckinsByStudentId(param);
		if(checkins.size()>0){
			model.addAttribute("ischeck", true);
		}
		
		return "dormitorycheck.page";
	}
	@RequestMapping(value = "/queryDormCheckins")
	public String queryDormCheckins (Model model, HttpServletRequest request){
		BaseInfo baseInfo  =(BaseInfo) request.getSession().getAttribute(UserSessionKeyEnum.BASE_USER_INFO.getValue());
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
		String time = df.format(new Date());
		int num=0;
		List<HzDormMember> members;
		List<HzTeacher> tlist=null;
		List<HzTeacher> contactlist;
		Map<String, Object> param = new HashMap();
		if (!baseInfo.isStudentUser()) return "dormitorycheckupdate.page";
		param.put("studentid", baseInfo.getStudent().getSno());
		param.put("date",time);
		
		members = hzCheckDormService.queryDormMember(param);
		Map<String, List<Object>> queryTparam = new HashMap();
		List<Object> temps = new ArrayList<Object>();
		int tempint = 0;
		for (HzDormMember temp : members) {
			
			temp.setDcstate(""+hzCheckDormService.checkDormMemberstateByStudentId(temp.getStudentId(), time));
			
			temp.setId(++tempint + "");
			temps.add(temp.getClassId());
		}
		queryTparam.put("clist", temps);
		if(temps.size()!=0){
			tlist = hzCommonService.queryTeachersByClassid(queryTparam);
			for (int i = 0; i < tlist.size(); i++) {
				num++;
				tlist.get(i).setId(num);
				tlist.get(i).setClassName(tlist.get(i).getClassName() + "(班主任)");
			}
		}
		contactlist = hzCheckDormService.queryDormContactByStudentId(param);
		for (int i = 0; i < contactlist.size(); i++) {
			num++;
			contactlist.get(i).setId(num);
			contactlist.get(i).setClassName("寝室联系人");
		}

		model.addAttribute("teacherlist", tlist);
		model.addAttribute("contactlist", contactlist);
		model.addAttribute("members", members);
		

		List checkins=hzCheckDormService.checkDormCheckinsByStudentId(param);
		if(checkins.size()>0){
			model.addAttribute("ischeck", true);
		}
		
		return "dormitorycheckupdate.page";
		
	}
	
	

	@RequestMapping(value = "/checkDormMenber")
	public String checkDormMember(Model model, HttpServletRequest request) {
		BaseInfo baseInfo  =(BaseInfo) request.getSession().getAttribute(UserSessionKeyEnum.BASE_USER_INFO.getValue());
		Map<String, Object> param = new HashMap();
		param.put("studentid", baseInfo.getStudent().getSno());
		List<HzDormMember> members = hzCheckDormService.queryDormMember(param);
		model.addAttribute("responsebody", members.size());
		return "dormitorycheck.page";
	}

	@ResponseBody
	@RequestMapping(value = "/checkDorm", method = RequestMethod.POST) // 要改成post
	public Map checkDorm(Model model, HttpServletRequest request) {
		BaseInfo baseInfo  =(BaseInfo) request.getSession().getAttribute(UserSessionKeyEnum.BASE_USER_INFO.getValue());
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
		String time = df.format(new Date());

		String students = request.getParameter("students");// 学生点到信息
		String checkStudentSno = baseInfo.getStudent().getSno();// 点到学生学号

//		checkStudentSno = "3333";
		int n = 0;
		if (StringUtils.isNotEmpty(checkStudentSno) && !students.contains("-")) {
			// 所有人都正常点到
			HzDormitoryCheckins temp = new HzDormitoryCheckins();
			temp.setStuId(checkStudentSno);
			temp.setDcstate(0);
			temp.setDcdate(new Date());
			n += hzCheckDormService.addDormCheck(temp);

		} else {// 有异常
			String[] sno_states = students.split(","); // 001_2,002_2
			if (sno_states.length > 0) {
				for (String s : sno_states) {
					String[] sno_state = s.split("-");
					if (sno_state.length == 2) {
						HzDormitoryCheckins temp = new HzDormitoryCheckins();
						temp.setStuId(sno_state[0]);
						temp.setDcstate(Integer.valueOf(sno_state[1]));
						temp.setDcdate(new Date());
						n += hzCheckDormService.addDormCheck(temp);
					}

				}
			}

		}

		// n+ = hzCheckDormService.addDormCheck(param);

		Map<String, Object> resultMap = new HashMap<String, Object>();

		if (n > 0) {
			resultMap.put("resultMsg", "提交成功!需修改请到修改页面修改`(*∩_∩*)′");
			resultMap.put("state", true);
			resultMap.put("showDiv", ".alert-success");
		} else {
			resultMap.put("resultMsg", "提交数据失败,请重试");
			resultMap.put("state", false);
			resultMap.put("showDiv", ".alert-danger");
		}

		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping(value = "/checkDormUpdate", method = RequestMethod.POST) // 要改成post
	public Map checkDormupdate(Model model, HttpServletRequest request) {
		BaseInfo baseInfo  =(BaseInfo) request.getSession().getAttribute(UserSessionKeyEnum.BASE_USER_INFO.getValue());
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
		String time = df.format(new Date());
		
		List<HzDormMember> members;
		Map<String, Object> param = new HashMap();
		param.put("studentid", baseInfo.getStudent().getSno());
		param.put("date",time);
		
		members = hzCheckDormService.queryDormMember(param);
		for (HzDormMember temp : members) {
			temp.setDcstate(""+hzCheckDormService.checkDormMemberstateByStudentId(temp.getStudentId(), time));
		}
		
		String students = request.getParameter("students");// 学生点到信息
		String checkStudentSno = baseInfo.getStudent().getSno();


		int n = 0;
		if (StringUtils.isNotEmpty(checkStudentSno) && !students.contains("-")) {
			// 所有人都正常点到
			for(HzDormMember tempm : members){
				if(!"0".equals(tempm.getDcstate())){
					n +=hzCheckDormService.updateDormCheck(tempm.getStudentId(), 0, time);
				}
			}

		} else {// 有异常
			String[] sno_states = students.split(","); // 001_2,002_2
			if (sno_states.length > 0) {
				for (String s : sno_states) {
					String[] sno_state = s.split("-");
					if (sno_state.length == 2) {
						
						
						if(0<hzCheckDormService.checkDormMemberischeckByStudentId(sno_state[0], time)){
							n +=hzCheckDormService.updateDormCheck(sno_state[0], Integer.valueOf(sno_state[1]), time);
						}else{
							HzDormitoryCheckins temp = new HzDormitoryCheckins();
							temp.setStuId(sno_state[0]);
							temp.setDcstate(Integer.valueOf(sno_state[1]));
							temp.setDcdate(new Date());
							n += hzCheckDormService.addDormCheck(temp);
						}
						

					}

				}
			}

		}

		// n+ = hzCheckDormService.addDormCheck(param);

		Map<String, Object> resultMap = new HashMap<String, Object>();

		if (n > 0) {
			resultMap.put("resultMsg", "修改成功!`(*∩_∩*)′");
			resultMap.put("state", true);
			resultMap.put("showDiv", ".alert-success");
		} else {
			resultMap.put("resultMsg", "提交数据失败,请重试");
			resultMap.put("state", false);
			resultMap.put("showDiv", ".alert-danger");
		}

		return resultMap;
	}
}
